Support aspect=TRUE in _gdk_pixbuf_new_from_stream_at_scale
authorAlexander Larsson <alexl@redhat.com>
Wed, 5 Feb 2020 16:44:47 +0000 (17:44 +0100)
committerAlexander Larsson <alexl@redhat.com>
Thu, 6 Feb 2020 08:38:25 +0000 (09:38 +0100)
The icon theme test failed without this, and we *should* handle
it if we're accepting the argument.

gtk/tools/gdkpixbufutils.c

index c706d720a85d194cc9adc44b5987c2855aab7b4d..0d93b3237e1dc2a1c923bac722aec6b6e5576851 100644 (file)
@@ -126,7 +126,28 @@ size_prepared_cb2 (GdkPixbufLoader *loader,
 {
   int *scales = data;
 
-  gdk_pixbuf_loader_set_size (loader, scales[0], scales[1]);
+  if (scales[2]) /* keep same aspect ratio as original, while fitting in given size */
+    {
+      double aspect = (double) height / width;
+
+      /* First use given width and calculate size */
+      width = scales[0];
+      height = scales[0] * aspect;
+
+      /* Check if it fits given height, otherwise scale down */
+      if (height > scales[1])
+        {
+          width *= (double) scales[1] / height;
+          height = scales[1];
+        }
+    }
+  else
+    {
+      width = scales[0];
+      height = scales[1];
+    }
+
+  gdk_pixbuf_loader_set_size (loader, width, scales[1]);
 }
 
 GdkPixbuf *
@@ -140,7 +161,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream  *stream,
 {
   GdkPixbufLoader *loader;
   GdkPixbuf *pixbuf;
-  int scales[2];
+  int scales[3];
 
   if (format)
     {
@@ -153,6 +174,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream  *stream,
 
   scales[0] = width;
   scales[1] = height;
+  scales[2] = aspect;
   g_signal_connect (loader, "size-prepared",
                     G_CALLBACK (size_prepared_cb2), scales);